##############################
# MAKE FOR ROTAT
##############################

sys := $(shell uname)
ifneq (,$(findstring CYGWIN,$(strip $(sys))))
   SYSTYPE := "cygwin_f90"
else
#  SYSTYPE := "ifort"
   SYSTYPE := "ifx"
#  SYSTYPE := "gfortran"
endif
# SYSTYPE="ifort"
# SYSTYPE="cygwin_ifort"
# SYSTYPE="cygwin_f90"
UnixOrWin = "unix" # default value

########################################
## For Intel fortran
ifeq ($(SYSTYPE),"ifort")
  FC = ifort  # Intel fortran
#   FFLAGS = -c -save -zero -132 -O3 -fpe0 -no-ftz
#   FFLAGS = -c -save -zero -132 -O3 -fp-model strict
#   FFLAGS = -c -save -zero -132 -fp-model strict -g -fpe0 -CB -traceback
  FFLAGS = -c -132 -save -g -traceback
# FFLAGS = -c -save -zero -132 -O3
#  FFLAGS = -c -save -zero -132 -fpe0 -no-ftz -CB -g -traceback # -- for debug
  #FFLAGS = -c -O3 -save -zero -tpp7   -ip # -xW # -- optimize for Pentium
  #FFLAGS =   -c -save -static -zero -O2 -fpe0 -no-ftz -traceback # -CB -traceback
  #LDFLAGS = -i-static
#  FFLAGS = -c -save -fpe0 -no-ftz -g -CB -traceback -inline_debug_info  -DD
#   LIBS= $(HOME)/lib/libsparse.a $(HOME)/lib/liblnag.a  -lpng -lpgplot -lX11
#  LIBS= $(HOME)/lib/libsparse.a -lpng -lpgplot -lX11
  LIBS= -lpng -lpgplot -lX11
#   LIBS= $(HOME)/lib/libsparseStr.a $(HOME)/lib/liblnagStr.a  -lpng -lpgplot -lX11
#  LIBDIRSYS =
#  LIBSM = /usr/local/lib/libplotsub.a \
#        /usr/local/lib/libdevices.a \
#        /usr/local/lib/libutils.a
#  -L/usr/lib -lX11

#-lsocket -lnsl

# -L /opt/X11R6.4/lib -lX11

  UnixOrWin := "unix"
endif

########################################
########################################
## For Intel fortran
ifeq ($(SYSTYPE),"ifx")
  FC = ifx  # Intel fortran
  FFLAGS = -c -132 -save -g -traceback
  LIBS= -lpng -lpgplot -lX11

  UnixOrWin := "unix"
endif

########################################

########################################
## For Intel fortran
ifeq ($(SYSTYPE),"cygwin_ifort")
  FC = ifl  # Intel fortran
  FFLAGS = -c -save -zero  -tpp7   -ip  -static -fast
#  FFLAGS = -c -O3 -save -zero -tpp7   -ip
#  FFLAGS = -compile_only -static   -architecture:k7 -optimize:5 -fast -tune:k7 # for optimization Pentium
#  FFLAGS = -c -static -debug:full -check:bounds,power,overflow -traceback -list -show:all
  LDFLAGS =
#  LDFLAGS = /libs:static
  LIBS = ..\\lib\\liblnag.a	..\\lib\\libspars.a
  LIBDIRSYS = /I"/cygdrive/c/Program Files/Intel/Compiler70/IA32/Lib/"
#  LIBDIRSYS = /I"/cygdrive/c/Program Files/Intel/Compiler70/IA32/Lib/"
  UnixOrWin := "win"
endif


########################################
## For Visial Compaq Fortran
ifeq ($(SYSTYPE),"cygwin_f90")
  FC = f90  # Compaq fortran
  LINK = link.exe
  # FFLAGS = -compile_only -static  -fast -tune:k7
  LDFLAGS = -architecture:k7 -optimize:5 -fast -tune:k7 # /ignore:505
  FFLAGS = -compile_only -static   -architecture:k7 -optimize:5 -fast -tune:k7 # for optimization Pentium
#  FFLAGS = -c -static -debug:full -check:bounds,power,overflow -traceback -list -show:all
  LIBS = d:\\prg\\lib\\libsparscvf.a d:\\prg\\lib\\liblnagcvf.a
#  LIBS = ..\\lib\\liblnagcvf.a	..\\lib\\libsparscvf.a
  LIBDIRSYS =
#  LIBDIRSYS = /libpath:"c:\\Program Files\\Microsoft Visual Studio\\DF98\\LIB\\" /libpath:"c:\\Program Files\\Microsoft Visual Studio\\VC98\\LIB\\"

#  LIBDIRSYS = "c:/Program Files/Microsoft Visual Studio/DF98/LIB/"
  UnixOrWin := "win"
endif

########################################

## For gfortran
ifeq ($(SYSTYPE),"gfortran")
  FC = gfortran
  FFLAGS = -c -O2 -ffixed-line-length-132 -fexpensive-optimizations -finit-local-zero -fno-automatic -Wall -ffpe-trap=zero,overflow
  # FFLAGS = -c -g -fbounds-check -finit-local-zero -fno-automatic # for g77
  LDFLAGS =
#   LIBS= $(HOME)/lib/gfsparse.a $(HOME)/lib/gflnag.a -L/home/blinn/APUS/bin/pgplot -lpgplot -lpng -lX11
  LIBS=  -lpng -lpgplot -lX11
  LIBDIRSYS =
  LIBSM = /usr/local/lib/libplotsub.a \
        /usr/local/lib/libdevices.a \
        /usr/local/lib/libutils.a -L/usr/lib -lX11
  UnixOrWin := "unix"
endif
########################################


########################################
## For Windows
ifeq ($(UnixOrWin),"win")
 FFLAGS += -object:$@
 LDFLAGS += /exe:
 slash_l := \\
endif
########################################
## For Linux
ifeq ($(UnixOrWin),"unix")
 LDFLAGS += -o
 slash_l := /
endif
#-------------------------------------------------------
#---
#-------------------------------------------------------

# OBJS_SPARSE := m28bys.o m28y12.o m28cys.o m28cyn.o m30y12.o

PROGSHYUDINRHO= shyudinrho.exe
PROGSHYUDINRHO:= $(subst /,$(slash_l),$(PROGSHYUDINRHO))
FILES_SHYUDINRHO = rotLawIR.f myrotlaw.for modulesRotOld.f eosyudin.f \
   poly2timmes.f OmegaLaw.f90 Stability.f90 lnag.f  rho0Yudinmain.f
OBJS_SHYUDINRHO:= $(patsubst %.f,%.o, $(FILES_SHYUDINRHO))
# lnag.f instead of  vecsubs.trf interpolants.f  polyfits.f $(OBJS_SPARSE) $(OBJS_NAG)

PROGPLOT = plot2rot.exe # main plotting in cylinder coordinates
PROGPLOT := $(subst /,$(slash_l),$(PROGPLOT))
FILES_PLOT = modulesRotOld.f plot2rot.f plot2db3.f pgcell.f dsqinf.f pgxtalm.f lnag.f
OBJS_PLOT := $(patsubst %.f,%.o, $(FILES_PLOT))

DEL = rm -f

TREFOR = trf
# it is very important to show exact path for shared librarEOSies with -Wl,-rpath option below, because different compilers use different libraries

# Lines from here on down should not need to be changed.  They are the
# actual rules which make uses to build the executable
#


# for ctrf:
TRFFLAGS = -nfs

.IGNORE:
%.o : %.trf
	$(TREFOR) $(TRFFLAGS) $<
	$(FC) $(FFLAGS) $(subst /,$(slash_l),$(patsubst %.trf,%.f,$<))

#.SUFFIXES: $(SUFFIXES) .f

%.o: %.f
	$(FC) $(FFLAGS) $(subst /,$(slash_l),$<)

%.mod : %.o
	@if [! -f $@ ]; then \
	  rm $< \
	  $(MAKE) $< \
	fi

%.o : %.f90
	$(FC) $(FFLAGS) $(subst /,$(slash_l),$<)


all: help
	@echo current compiler: $(FC)
	@echo and its options : $(FFLAGS)


shyudinrho: $(OBJS_SHYUDINRHO)
	$(FC) $(LDFLAGS) $(PROGSHYUDINRHO) $(OBJS_SHYUDINRHO) $(LIBS) $(LIBDIRSYS)

plot: $(OBJS_PLOT)
	$(FC) $(LDFLAGS) $(PROGPLOT) $(OBJS_PLOT) $(LIBS)


help:
	@echo "You can do: "
	@echo " shyudinrho     --  compile shyudinrho: rho0 series -- no supermongo, sparse M28Y12"
	@echo " plot           --  compile plot2rot main plotting"
	@echo " cleandata      --  DEL nohup.out core* fort.*"
	@echo " clean          --  DEL *.o *.il and *.f from trf"
	@echo " cleanall       --  DEL PROGROT *.o *.il  *.mod *.lst"
	@echo " help           --  print this help"

#
# Here are all the dependencies:
#


######
# ROTAT
######
rho0Yudinmain.o  : rho0Yudinmain.f modulesRotOld.o
rotLawmain.o    :  rotLawmain.f
rotLawIR.o      :  rotLawIR.f modulesRotOld.o
eosyudin.o      :  eosyudin.f
poly2timmes.f   :  implno.dek const.dek
modulesRotOld.o :  modulesRotOld.f

######
# PLOT
######
plot2rot.o      :  plot2rot.f
plot2db3.o      :  plot2db3.f
pgcell.o        :  pgcell.f
dsqinf.o        :  dsqinf.f
pgxtalm.o       :  pgxtalm.f


cleandata:
	$(DEL) nohup.out core* fort.*

clean:
	$(DEL) *.o  *.il


cleanall:
	$(DEL) $(PROGSHYUDINRHO) \
	$(PROGPLOT)  \
	*.o  *.il
	$(DEL) *.mod *.lst
